package com.tanqin.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.tanqin.dao.OrderSettingDao;
import com.tanqin.pojo.OrderSetting;
import com.tanqin.service.OrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service(interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {
    @Autowired
    OrderSettingDao orderSettingDao;

    @Override
    public void add(List<OrderSetting> orderSettingList) {
        // 判断是否非空
        if (orderSettingList != null && orderSettingList.size() > 0) {
            // 遍历集合
            for (OrderSetting orderSetting : orderSettingList) {
                // 根据预约项的日期查询预约项的数量
                long countByOrderDate = orderSettingDao.findCountByOrderDate(orderSetting.getOrderDate());
                if (countByOrderDate > 0) {
                    // 存在对应预约项时，进行编辑可预约数量操作
                    orderSettingDao.editNumberByOrderDate(orderSetting);
                } else {
                    // 不存在对应预约项时，进行添加操作
                    orderSettingDao.add(orderSetting);
                }
            }
        }
    }

    @Override
    public List<Map> getOrderSettingByMonth(String date) {
        String begin = date + "-1";
        String end = date + "-31";
        Map<String, String> map = new HashMap<>();
        map.put("begin", begin);
        map.put("end", end);
        List<OrderSetting> orderSettingList = orderSettingDao.getOrderSettingByMonth(map);
        ArrayList<Map> list = new ArrayList<>();
        if (orderSettingList != null && orderSettingList.size() > 0) {
            Map<String, Object> m = null;
            for (OrderSetting orderSetting : orderSettingList) {
                m = new HashMap<>();
                m.put("date", orderSetting.getOrderDate().getDate()); // 获取日期数字
                m.put("number", orderSetting.getNumber());
                m.put("reservations", orderSetting.getReservations());
                list.add(m);
            }
        }
        return list;
    }

    @Override
    public void editNumberByDate(OrderSetting orderSetting) {
        long count = orderSettingDao.findCountByOrderDate(orderSetting.getOrderDate());
        if (count > 0) {
            // 编辑
            orderSettingDao.editNumberByOrderDate(orderSetting);
        } else {
            // 添加
            orderSettingDao.add(orderSetting);
        }
    }
}
